package com.wan.swaggerdemo.controller;

import com.wan.swaggerdemo.pojo.User;
import com.wan.swaggerdemo.pojo.UserParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户管理接口-GET请求")
@RestController
public class GetUserController {

    /**
     * 1、从header中获取参数
     * @return
     */
    @Operation(summary = "从header中获取参数")
    @GetMapping("/getParamFromHeader")
    public User getParamFromHeader(@Parameter(name = "name", description = "用户名", required = true,
            in = ParameterIn.HEADER, example = "zhangsan") @RequestHeader("name") String name) {
        User user = new User();
        user.setName(name);
        user.setPassword("123456");
        return user;
    }

    /**
     * 2、单个或多个入参
     * @param name
     * @return
     */
    @Operation(summary = "单个或多个入参")
    @GetMapping("/getUserByName")
    public String getUserByName(@RequestParam @Parameter(name = "name", description = "用户名", required = true,
            in = ParameterIn.QUERY, example = "zhangsan") String name) {
        return name;
    }


    /**
     * 3、参数在path路径中(restful风格)
     * @param id
     * @return
     */
    @Operation(summary = "参数在path路径中(restful风格)")
    @GetMapping("/getUserRestful/{name}")
    public User getUserRestful(@PathVariable("name") @Parameter(name = "id", description = "用户名", required = true,
            in = ParameterIn.PATH, example = "zhangsan") Long id) {
        User user = new User();
        user.setName("kelvin" + id);
        user.setPassword("123456");
        return user;
    }

    /**
     * 4、参数为user对象
     * @param user
     * @return
     */
    @Parameters({
            @Parameter(name = "name", description = "用户名", required = true),
            @Parameter(name = "password", description = "密码")
    })
    @Operation(summary = "参数为user对象")
    @GetMapping("/getUserByUserParam")
    public UserParam getUserByUserParam(@ParameterObject UserParam user) {
        return user;
    }

    /**
     * 5、返回响应码
     * @param name
     * @return
     */
    @Operation(summary = "返回响应码")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "40005", description = "失败",content = @Content)
    })
    @GetMapping("/getUserResponseCode")
    public String getUserResponseCode(@RequestParam @Parameter(name = "name", description = "用户名", required = true,
            in = ParameterIn.QUERY, example = "zhangsan") String name) {
        return name;
    }

}
